package com.atguigu.mapreduce.demo.wordcount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @version V1.0
 * @author: guangcheng
 * @date: 2021/11/13 20:03
 * @description:
 */
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    private Text outK = new Text();
    private IntWritable outV = new IntWritable(1);


    /**
     * KEYIN, map阶段输入的key的类型：LongWritable
     * VALUEIN,map阶段输入value类型：Text
     * KEYOUT,map阶段输出的Key类型：Text
     * VALUEOUT,map阶段输出的value类型：IntWritable
     */
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        // value代表一行, 一行
        String text = value.toString();

        //转为string, 利用String丰富的API
        String[] words = text.split(" ");

        //处理每一个单词变为该形式 (K, 1)
        for (String word : words) {

            outK.set(word);

            context.write(outK, outV);
        }

    }
}

